<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="merchantId">
        <el-select v-model="queryParams.merchantId" style="width:220px;" placeholder="请选择商户"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="mer in merchantList" :key="mer.id"
                     :label="mer.defindName ? mer.defindName : mer.merchantName"
                     :value="mer.merchantId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="shopId">
        <el-input v-model="queryParams.shopId" placeholder="请输入店铺ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item prop="itemId">
        <el-input v-model="queryParams.itemId" placeholder="请输入商品Id" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="selectSync">
          同步商品
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :disabled="multiple"
                   @click="handleDiscount">添加折扣
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="forceDelete">
          强制删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table ref="multipleTable" v-loading="loading" :data="publishList" @row-click="handleRowClick"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" width="200" prop="publishTaskId"/>
      <el-table-column label="商品名称" align="center" :show-overflow-tooltip="true" prop="proName"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope"><span v-if="scope.row.status == -1" style="color: red">发布失败</span>
          <span v-if="scope.row.status == 0" style="color: gray">已发布</span>
          <span v-if="scope.row.status > 0" style="color: green">发布成功</span>
        </template>
      </el-table-column>
      <el-table-column label="地区" align="center" prop="region"/>
      <el-table-column label="商品状态" align="center" prop="itemStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.itemStatus == 'DELETED'" type="danger" size="mini">商品已被删除</el-tag>
          <el-tag v-if="scope.row.itemStatus == 'NORMAL'" type="success" size="mini">正常</el-tag>
          <el-tag v-if="scope.row.itemStatus == 'BANNED'" type="warning" size="mini">审核中</el-tag>

        </template>
      </el-table-column>
      <el-table-column label="失败原因" align="center" :show-overflow-tooltip="true" prop="failedReason"/>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>
<script>
import {
  syncSelect,
  listPublish,
  addAllDiscount,
  delPublish,
  forceDelPublish,
  addDiscount,
} from "@/api/store/publish";
import Cookies from "js-cookie";
import {listAllMerchant} from "@/api/shop/merchant";

export default {
  name: "Publish",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //全球商户列表
      merchantList: [],
      // 商品发布表格数据
      publishList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        merchantId: null,
        publishTaskId: null,
        status: null,
        region: null,
        shopId: null,
        itemId: null,
        itemStatus: null,
      },
    };
  },
  created() {

    this.getMerchantList();
  },
  methods: {
    /** 查询商品发布列表 */
    getList() {
      this.loading = true;
      listPublish(this.queryParams).then((response) => {
        this.publishList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMerchantList() {
      listAllMerchant().then((response) => {
        this.merchantList = response.data;
        //默认选中
        if (Cookies.get('merchantId')) {
          this.queryParams.merchantId = +Cookies.get('merchantId');
        }
        this.getList();
      });
    },

    // 同步选中
    selectSync() {
      syncSelect(this.ids).then(() => {
        this.getList();
        this.$modal.msgSuccess("同步成功");
      });
    },
    //添加折扣
    handleDiscount() {
      addDiscount(this.ids).then(() => {
        this.getList();
        this.$modal.msgSuccess("添加折扣成功");
      });
    },
    handleAllDiscount() {
      if (this.queryParams.merchantId == null) {
        this.$modal.msgWarning("请选择商户");
        return;
      }
      addAllDiscount(this.queryParams.merchantId).then((response) => {
        this.getList();
        this.$modal.msgSuccess("添加折扣成功");
      });
    },
    //点击行触发，选中或不选中复选框
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.merchantId) {
        Cookies.set('merchantId', this.queryParams.merchantId);
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const publishTaskId = row.publishTaskId || this.ids;
      this.$modal
        .confirm('是否确认删除发布记录为"' + publishTaskId + '"的数据项？')
        .then(function () {
          return delPublish(publishTaskId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 强制删除按钮操作 */
    forceDelete(row) {
      const publishTaskId = row.publishTaskId || this.ids;
      this.$modal
        .confirm('是否确认删除发布记录为"' + publishTaskId + '"的数据项？删除之后不可以恢复！！！')
        .then(function () {
          return forceDelPublish(publishTaskId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.publishTaskId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSyncAll() {
      //console.log(this.queryParams.merchantId);
      if (this.queryParams.merchantId == null) {
        this.$modal.msgWarning("请选择商户");
        return;
      }
      syncItemAll(this.queryParams.merchantId).then((response) => {
        this.getList();
        this.$modal.msgSuccess("更新成功");
      });
    },
  },
};
</script>
